package com.atguigu.eduservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.R;
import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.utils.MapObjectUtil;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.vo.TokenMemberVo;
import com.atguigu.eduservice.mapper.EduCommentMapper;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-02-21
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Override
    public FrontPageBean getFrontComment(Integer pageNo, Integer pageSize,String courseId) {
        Page<EduComment> pageParam = new Page<>(pageNo,pageSize);
        QueryWrapper<EduComment> qw = new QueryWrapper<>();
        qw.eq("course_id",courseId);
        qw.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, qw);

        FrontPageBean frontPageBean = copyPropertiesFrontPage(pageParam);
        return frontPageBean;
    }

    /**
     * 拼装属性
     * @param pageParam
     * @return
     */
    private FrontPageBean copyPropertiesFrontPage(Page pageParam) {
        FrontPageBean frontPageBean = new FrontPageBean();
        frontPageBean.setRecords(pageParam.getRecords());
        frontPageBean.setCurrent(pageParam.getCurrent());
        frontPageBean.setPages(pageParam.getPages());
        frontPageBean.setSize(pageParam.getSize());
        frontPageBean.setTotal(pageParam.getTotal());
        frontPageBean.setHasNext(pageParam.hasNext());
        frontPageBean.setHasPrevious(pageParam.hasPrevious());
        return frontPageBean;
    }

    @Override
    public void commitComment(EduComment eduComment) {
        baseMapper.insert(eduComment);
    }

}
